﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PapnKukn.Silverlight.SilverKnight.ScoreService;
using System.ServiceModel;

namespace PapnKukn.Silverlight.SilverKnight
{
    public partial class Score : UserControl
    {
        #region Constructor
        public Score()
        {
            InitializeComponent();
            loadScore();
        }
        #endregion

        #region Methods
        public void Reload()
        {
            requestScore();
        }

        private void requestScore()
        {
            try
            {
                if (AppConfig.AppHostBase != null)
                {
                    BasicHttpBinding binding = new BasicHttpBinding();
                    EndpointAddress address = new EndpointAddress(AppConfig.ServiceUrl);
                    ScoreServicePortTypeClient client = new ScoreServicePortTypeClient(binding, address);
                    client.GetScoresCompleted += new EventHandler<GetScoresCompletedEventArgs>(client_GetScoresCompleted);
                    client.GetScoresAsync();
                }
                else
                {
                    ScoreServicePortTypeClient client = new ScoreServicePortTypeClient();
                    client.GetScoresCompleted += new EventHandler<GetScoresCompletedEventArgs>(client_GetScoresCompleted);
                    client.GetScoresAsync();
                }
            }
            catch (Exception ex)
            {
                Logger.ShowException(ex);
            }
        }

        protected void client_GetScoresCompleted(object sender, GetScoresCompletedEventArgs e)
        {
            try
            {
                if (e.Error == null && !string.IsNullOrEmpty(e.Result))
                {
                    string result = e.Result;
                    string[] rows = result.Split('\n');
                    int count = rows.Length;
                    if (count > 0)
                    {
                        list.Children.Clear();
                        for (int i = 0; i < count && i < 5; i++)
                        {
                            string[] cols = rows[i].Split('~');
                            if (cols.Length >= 3)
                            {
                                addRow(cols[1], cols[2]);
                            }
                        }
                    }
                }
            }
            catch
            {
            }
        }

        private void loadScore()
        {
            list.Children.Clear();
            addRow("unknown", "0");
            //addRow("unknown", "0");
            //addRow("unknown", "0");
            //addRow("unknown", "0");
            //addRow("unknown", "0");
            requestScore();
        }

        private void addRow(string name, string score)
        {
            if (name != null && name.Length > 10)
            {
                name = name.Remove(10, name.Length - 10);
            }

            Canvas canvas = new Canvas();
            canvas.Opacity = 0.4;
            canvas.Width = 150;
            canvas.Height = 18;

            TextBlock lblName = new TextBlock();
            lblName.TextAlignment = TextAlignment.Left;
            lblName.TextWrapping = TextWrapping.NoWrap;
            lblName.Foreground = new SolidColorBrush(Colors.Black);
            lblName.Width = 150;
            lblName.Text = name;
            canvas.Children.Add(lblName);

            TextBlock lblScore = new TextBlock();
            lblScore.TextAlignment = TextAlignment.Right;
            lblScore.TextWrapping = TextWrapping.NoWrap;
            lblName.Foreground = new SolidColorBrush(Colors.Black);
            lblScore.Width = 150;
            lblScore.Text = score;
            canvas.Children.Add(lblScore);

            list.Children.Add(canvas);
        }
        #endregion
    }
}
